package com.it.selfAfter.controller;

import com.it.selfAfter.dto.RegisterDTO;
import com.it.selfAfter.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.it.selfAfter.service.UserService;
import org.springframework.web.multipart.MultipartFile;

/**
 *  用户相关接口
 * @return null
 * @author: CaoYouGen
 * @DateTime: 2023/6/8 16:30
 * @DocView.Name 用户相关接口
 */
@CrossOrigin(origins = "*") // 解决跨域问题
@RestController
@RequestMapping("/api/user")
@Slf4j
@Api(tags = "用户相关接口,提供用户的增删改查功能")
public class UserContorller {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "注册/新增用户", notes = "注册/新增一个用户")
    @PostMapping("/add")
    public Result<?> addLogin(@ApiParam(value = "用户信息", required = true) @RequestBody RegisterDTO registerDTO){
        return userService.add(registerDTO);
    }

    @ApiOperation(value = "根据手机号判断用户是否存在", notes = "根据手机号判断用户是否存在")
    @PostMapping("/getUserByPhone")
    public Result<?> getUserByPhone(@ApiParam(value = "用户手机号", readOnly = true) @RequestBody RegisterDTO registerDTO){
        log.info("----------------------"+registerDTO.getPhone());
        return userService.getUserByPhone(registerDTO.getPhone());
    }


    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录")
    public Result<?> login(@ApiParam(value = "用户信息", required = true) @RequestBody RegisterDTO registerDTO){
        return userService.login(registerDTO);
    }

    @ApiOperation(value = "上传头像的照片", notes = "上传头像")
    @PostMapping("/upload")
    public Result uploadAvatar(@ApiParam(value = "文件信息和手机号", required = true) @RequestParam("file") MultipartFile file, @RequestParam("phone") Long phone) {
        return  userService.uploadAvatar(file,phone);
    }

    @ApiOperation(value = "根据uuid获取用户信息", notes = "根据uuid获取用户信息")
    @PostMapping("/getUsersByUserId")
    public Result<?> getUsersByUserId(@ApiParam(value = "用户uuid", readOnly = true) @RequestBody RegisterDTO registerDTO){
        log.info("----------------------"+registerDTO.getUserId());
        return userService.getUsersByUserId(registerDTO.getUserId());
    }
    @ApiOperation(value = "根据手机号更新用户uuid", notes = "根据手机号更新用户uuid")
    @PostMapping("/updateUUIDByPhone")
    public Result<?> updateUUIDByPhone(@ApiParam(value = "用户uuid", readOnly = true) @RequestBody RegisterDTO registerDTO){
        log.info("----------------------"+registerDTO.getUuid());
        return userService.updateUUIDByPhone(registerDTO.getPhone(), registerDTO.getUuid());
    }


}